<?php

class SchedulerService extends BaseService {

	public function isInStandbyMode() {
        $scheduler_control = ScheduleControl::model()->find();

        if($scheduler_control == null) {
            return false;
        }
        return $scheduler_control->suspend == 0 ? false : true;
	}
	
	public function editStandbyMode($flag) {
		return SchedulerControlModel::model()->update(array('suspend' => $flag)); 
	}

	public function getBaseDate() {
		
		$conditions = array(
			'order'		=>		'date DESC',
		);
		
        $scheduler_log = Schedule::model()->find($this->toCriteria($conditions));
        if($scheduler_log != null) {
            return $scheduler_log->date;
        }
        else {
            return false;
        }
	}

	public function getSchedulerLogs($offset, $count) {
		$scheduler_logs = Scheduler::model()->page($offset, $count)->findAll();
		return $this->toArray($scheduler_logs);
	}

	public function addSchedulerLog(array $log) {
		try {
			$newLogModel = new Schedule;
			$newLogModel->setAttributes($log, false);
			$status = $newLogModel->save();
	
			return $status;
		}
		catch(Exception $e) {
			Yii::log("add scheduler date error:" . $e->getMessage(), CLogger::LEVEL_ERROR);
			return false;
		}
	}
	
	public function addCommand($command) {
        try {
            $newCommand = new Command;
            $newCommand->setAttributes($command, false);
            $status = $newCommand->save();
            return $status == true ? $newCommand->cmd_id : $status;
        } 
        catch(Exception $e) {
			Yii::log("add Command log error:" . $e->getMessage(), CLogger::LEVEL_ERROR);
			return false;
		}
	}
	
	public function getCommands($conditions) {
		$criteria = $this->toCriteria($conditions);
		try {
			$commands = Command::model()->findAll($criteria);
		 
			return $this->toArray($commands);
		}
		catch (Exception $e) {
			Yii::log("get commands error:" . $e->getMessage(), CLogger::LEVEL_ERROR);
			return false;
		}
	}
	
	public function getSubmitCommands() {
		$condition = array(
			'condition'	=>	array(
				array('state', '=', CmdState::Submit),
			),
			'order'	=>	'cmd_id',
		);
		
		return $this->getCommands($condition);
	}
	
	public function editCommandAttributes($cmdId, $attributes) {
		try {		
			return Command::model()->updateByPk($cmdId, $attributes);
		}
		catch (Exception $e) {
			Yii::log("edit command attributes error:" . $e->getMessage(), CLogger::LEVEL_ERROR);
			return false;
		}
	}
	
	public function selectExecutorMachine() {
		try {
			$machines = ExecutorMachine::model()->findAll('state ='. MachineState::Online);
			$count = count($machines);
				
			if($count > 0) {			
				$index = mt_rand(0, $count - 1);
				$machine = $machines[$index];
				
				return $machine->toArray();
			}
			else {
				return false;
			}
		}
		catch (Exception $e) {
			Yii::log("select executor machine error:" . $e->getMessage(), CLogger::LEVEL_ERROR);
			return false;
		}
	}
	
	public function getExecutorMachineByName($hostname) {
		$executorMachine = ExecutorMachine::model()->findByAttributes(array(
			'hostname'	=>	$hostname,
		));
		return $executorMachine->toArray();
	}
	
	public function getExecutorMachineById($executorId) {
		$executorMachine = ExecutorMachine::model()->findByPk($executorId);
		return $executorMachine->toArray();
	}
	
	public function updateCurrentCount($executorId, $increase = true) {
		$sql = 'UPDATE executor_machine SET ';
		if ($increase) {
			$sql .= 'current_workers = current_workers + 1 ';
		}
		else {
			$sql .= 'current_workers = current_workers - 1 ';
		}
		
		$sql .= "WHERE executor_id = $executorId";
		try {
			$db = ExecutorMachine::model()->dbConnection;
			$ret = $db->createCommand($sql)->execute();
			return $ret == 1 ? true : false;
		}
		catch(Exception $e){
			Yii::log('update executor machine current workers failed, message:' . $e->getMessage(), CLogger::LEVEL_ERROR);
			return false;
		}
	}
	
}
